<template>
  <el-dialog
    :title="!dataForm.communityActivityId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="详情地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="详情地址"></el-input>
    </el-form-item>
    <el-form-item label="点赞数量" prop="likes">
      <el-input v-model="dataForm.likes" placeholder="点赞数量"></el-input>
    </el-form-item>
    <el-form-item label="浏览数量" prop="views">
      <el-input v-model="dataForm.views" placeholder="浏览数量"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除0:否1:是" prop="delStauts">
      <el-input v-model="dataForm.delStauts" placeholder="是否删除0:否1:是"></el-input>
    </el-form-item>
    <el-form-item label="留言数量" prop="numberOfMessages">
      <el-input v-model="dataForm.numberOfMessages" placeholder="留言数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          communityActivityId: 0,
          userId: '',
          createTime: '',
          address: '',
          likes: '',
          views: '',
          updateTime: '',
          delStauts: '',
          numberOfMessages: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '详情地址不能为空', trigger: 'blur' }
          ],
          likes: [
            { required: true, message: '点赞数量不能为空', trigger: 'blur' }
          ],
          views: [
            { required: true, message: '浏览数量不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStauts: [
            { required: true, message: '是否删除0:否1:是不能为空', trigger: 'blur' }
          ],
          numberOfMessages: [
            { required: true, message: '留言数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.communityActivityId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.communityActivityId) {
            this.$http({
              url: this.$http.adornUrl(`/leader/information/info/${this.dataForm.communityActivityId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.information.userId
                this.dataForm.createTime = data.information.createTime
                this.dataForm.address = data.information.address
                this.dataForm.likes = data.information.likes
                this.dataForm.views = data.information.views
                this.dataForm.updateTime = data.information.updateTime
                this.dataForm.delStauts = data.information.delStauts
                this.dataForm.numberOfMessages = data.information.numberOfMessages
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/leader/information/${!this.dataForm.communityActivityId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'communityActivityId': this.dataForm.communityActivityId || undefined,
                'userId': this.dataForm.userId,
                'createTime': this.dataForm.createTime,
                'address': this.dataForm.address,
                'likes': this.dataForm.likes,
                'views': this.dataForm.views,
                'updateTime': this.dataForm.updateTime,
                'delStauts': this.dataForm.delStauts,
                'numberOfMessages': this.dataForm.numberOfMessages
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
